package com.kgd.news.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.upload.MultipartRequest;
import com.jfinal.upload.UploadFile;
import com.kgd.news.entity.Banner;
import com.kgd.news.entity.Category;
import com.kgd.news.entity.News;
import com.kgd.news.entity.SysDict;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.DateUtils;
import com.kgd.news.utils.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/9/26
 * @Time: 下午11:12queryForm
 */
public class NewsController extends BaseController {

    @Override
    public void list() {
        String title = getPara("title");
        String cid = getPara("cid");
        String isbanner = getPara("isbanner");

        String type = "news";
        String sql = "from news where 1 = 1";
        if (!StringUtils.isEmpty(title)) {
            sql += " and title like '%" + title + "%'";
            type += title;
        }
        if (!StringUtils.isEmpty(cid)) {
            if ("-1".equals(cid)) {
                sql += " and categoryid is null" ;
            } else {
                sql += " and categoryid =" + cid;

            }
            type += cid;
        }/*else {
//请选择不处理，未分类咋么处理
            sql += " and categoryid = 0";
        }*/

        if (!StringUtils.isEmpty(isbanner)) {
            sql += " and isbanner = " + isbanner;
            type += isbanner;
        }
        sql += " order by id desc";
//        Page<News> data=  CacheKit.get("news", type);//type可以作为缓存的key的吧。
//        if (data == null) {
            Page<News> data = News.dao.paginate(getPageIndex(type), PAGE_SIZE, "select *", sql);
//            CacheKit.put("news",type,data);
//        }
        setAttr("title", title);
        setAttr("data", data);
        setAttr("cid", cid);
        setAttr("isbanner", isbanner);
        render("news_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        News dict = News.dao.findById(id);
        setAttr("news", dict);
        setAttr("categoryid", getPara("cid"));
        if (dict != null) {
//            setAttr("isbanner", dict.getIsbanner());
        }

        render("news_edit.html");
    }

    @Override
    public void save() {

        UploadFile uploadFile = getFile("img");
        String filePath = "";
        if (uploadFile != null && !StringUtils.isEmpty(uploadFile.getFileName())) {
            filePath = getFileUploadPath(uploadFile);
        }
        News sd = getModel(News.class, "news");
//      String tempContent=  sd.getContent();
//        try {
//            String  realContent =new String(tempContent.getBytes("ISO-8859-1"),"UTF-8");
//            sd.setContent(realContent);
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        if (!StringUtils.isEmpty(filePath)) {
            sd.setImg(filePath);
        }
        String url = sd.getVideourl();
        if (!StringUtils.isEmpty(url)&&url.endsWith("html")) {
            sd.setVideorealurl(url);
        }

        if(sd != null && sd.getId() != null){
            sd.update();
        }else{
            sd.setCreatetime(DateUtils.getCurDateStr());
            sd.save();
        }
        CacheKit.removeAll("news");
        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from news where id in (" + ids + ")");
        setAttr("categoryid", getPara("cid"));
        CacheKit.removeAll("news");
        list();
    }

}
